<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>Hello MUI</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../css/mui.min.css">
		<link rel="stylesheet" href="../../css/app.css">
		<style>
			h4.mui-ellipsis {
				line-height: 20px;
			}
			.readed {
				font-weight: normal;
			}
			.due {
				font-weight: normal;
				color: #8f8f94;

			}
			.mui-input-row {
				margin: 5px 5px 0;
			}
			.mui-input-row input, .mui-input-row textarea{
				margin-bottom: 0;
			}
			
			.mui-input-row select{
				-webkit-appearance: menulist-button;
				border: 1px solid rgba(0,0,0,.2)!important;
				border-radius: 3px;
				margin-bottom: 0;
				padding-left: 15px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 class="mui-title">快递代收</h1>
		</header>

		<div class="mui-content" style="background: white;">
			
			<div class="mui-input-row">
				<select id="house">
					<option value="">请选择房屋</option>
				</select>
			</div>			
			<div class="mui-input-row">
				<input type="text" class="mui-input-clear" placeholder="联系人" id="contactName">
			</div>
			<div class="mui-input-row">
				<input type="text" class="mui-input-clear" placeholder="电话" id="contactCellphone">
			</div>

			<div style="margin: 15px 5px 5px;">
				<button class="mui-btn mui-btn-success mui-btn-block" id="submit">申请代收快递</button>
			</div>

		</div>

	</body>
	<script src="../../js/mui.min.js"></script>
	<script src="../../js/template.js"></script>
	<script src="../../js/app.js"></script>
	<script src="../../js/_API.js"></script>
	<script>
		mui.init();
		
		var loadPeople = function(){
			var m = _API.getPeople();
			if(m) {
				document.getElementById("contactName").value = m.nickname;
				document.getElementById("contactCellphone").value = m.cellphone;
			}			
		}
		
		mui.plusReady(function(){
			_API.ajax2('community/house/findByPeople', {}, true)
			.success(function(json){
				loadPeople();
				var str = '<option value="">请选择房屋</option>';
				for(var i=0; i<json.data.length; i++) {
					var d = json.data[i];
					str += '<option value="' + d.id + '">' + d.allNum + '</option>'
				}
				document.getElementById("house").innerHTML = str;
			}).error(function(json){
	        	plus.nativeUI.toast(json.msgInfo);
			});
		});
		
		var getVal = function(){
			var data = {
				contactName : document.getElementById("contactName").value,
				contactCellphone : document.getElementById("contactCellphone").value,
				houseId : document.getElementById("house").value
			}
			var msg = {
				contactName : "请填写联系人，以便与您联系",
				contactCellphone : "请填写联系电话，以便与您联系",
				houseId : "请选择房屋"
			}
			for(var i in data) {
				if(!data[i]) {
					alert(msg[i]);
					return ;
				}
			}
			return data;
		}
		
		document.getElementById("submit").addEventListener("tap", function(){
			var data = getVal();
			if(!data) {
				return ;
			}
			
			_API.ajax2('express/create', data, true)
			.success(function(json){
				plus.nativeUI.toast('申请代收快递成功，请关注推送消息');
				mui.back();
			}).error(function(json){
	        	plus.nativeUI.toast(json.msgInfo);
			});
		});
	</script>
</html>